<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link rel="stylesheet" href="/js/elementui/index.css">
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>
<div id="app">
    <el-button type="primary" @click="dialogFormVisible = true">添加角色</el-button>
    <!--数据表格-->
    <el-table
            ref="roleTable"
            :data="roles"
            @select-all="selectAll"
            @current-change="changeCurrent"
            style="width: 100%">
        <el-table-column
                type="selection"
                width="50">
        </el-table-column>
        <el-table-column label="编号">
            <template slot-scope="scope">
                <span>{{ scope.row.id }}</span>
            </template>
        </el-table-column>
        <el-table-column label="角色名">
            <template slot-scope="scope">
                <span>{{ scope.row.roleName }}</span>
            </template>
        </el-table-column>
        <el-table-column label="角色描述">
            <template slot-scope="scope">
                <span>{{ scope.row.roleDescribe }}</span>
            </template>
        </el-table-column>
        <el-table-column label="创建人">
            <template slot-scope="scope">
                <span>{{ scope.row.roleCreateName }}</span>
            </template>
        </el-table-column>
        <el-table-column label="创建时间">
            <template slot-scope="scope">
                <span>{{ scope.row.roleCreateDate }}</span>
            </template>
        </el-table-column>
        <el-table-column label="状态">
            <template slot-scope="scope">
                <span>{{ scope.row.roleStatus }}</span>
            </template>
        </el-table-column>
        <el-table-column label="公司ID">
            <template slot-scope="scope">
                <span>{{ scope.row.companyId }}</span>
            </template>
        </el-table-column>
        <el-table-column label="修改人">
            <template slot-scope="scope">
                <span>{{ scope.row.roleUpdateName }}</span>
            </template>
        </el-table-column>
        <el-table-column label="修改时间">
            <template slot-scope="scope">
                <span>{{ scope.row.roleUpdateDate }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
            background
            layout="prev, pager, next"
            @current-change="loginRoles"
            :total="total"
            :current-page="current"
            :page-size="pageSize">
    </el-pagination>
    <el-dialog title="角色信息" :visible.sync="dialogFormVisible" @close="closeMyDialog">
        <el-form :model="role">
            <el-form-item label="角色名称" :label-width="formLabelWidth">
                <el-input v-model="role.roleName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" :label-width="formLabelWidth">
                <el-input v-model="role.roleDescribe" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="创建人" :label-width="formLabelWidth">
                <el-input v-model="role.roleCreateName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth">
                <el-input v-model="role.roleStatus" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="公司ID" :label-width="formLabelWidth">
                <el-input v-model="role.companyId" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="修改人" :label-width="formLabelWidth">
                <el-input v-model="role.roleUpdateName" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="abolishRole">取 消</el-button>
            <el-button type="primary" @click="saveFunction">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script src="/js/vue/vue.js"></script>
<script src="/js/axios/axios.min.js"></script>
<script src="/js/qs/qs.min.js"></script>
<script src="/js/elementui/index.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                role: {
                    id: '',
                    roleName: '',
                    roleDescribe:'',
                    roleCreateName:'',
                    roleStatus:'',
                    companyId:'',
                    roleUpdateName:''
                },
                roles: null,
                total:0,//总条数
                current:1,//当前条数
                pageSize:5, //每页条数
                current1: null, //当前点击的角色
                dialogFormVisible: false,//控制对话框的显示
                formLabelWidth: '120px' //表单标签的宽度

            }
        },
        mounted() {
            this.loginRoles(1);
        },
        methods: {
            loginRoles(current) {
                this.current=current;
                //加载角色
                axios.get("/roles?current="+current).then(value => {
                    this.roles = value.data.records;
                    this.total = value.data.total;
                    this.pageSize = value.data.size;
                    this.current = value.data.current;
                    console.log(value.data)
                })
            },
            handleEdit(index, row) {
                console.log(index, row);
                this.role = row;
                this.dialogFormVisible = true;
            },
            handleDelete(index, row) {
                this.open(row)
                console.log(index, row);
            },
            abolishRole(){
                this.dialogFormVisible = false;
                this.loginRoles(this.current)
            },
            saveFunction() {
                //判断有id进行更新，否则添加
                if (this.role.id == '') {
                    axios.post("/role",this.role).then(value => {
                        this.dialogFormVisible = false;

                    });
                } else {
                    axios.put("/role",this.role).then(value => {
                        this.dialogFormVisible = false;
                    });
                }
                this.loginRoles(this.current)
            },
            //关闭对话框
            closeMyDialog() {
                //清空数据
                this.role = {
                    id: '',
                    roleName: '',
                    roleDescribe:'',
                    roleCreateName:'',
                    roleStatus:'',
                    companyId:'',
                    roleUpdateName:''
                }
            },
            changeCurrent(row) {

                this.current1 = row;
                //取消所有的选择效果
                this.$refs.roleTable.clearSelection();
                //选中当前的行
                this.$refs.roleTable.toggleRowSelection(row);
            },
            //取消全选角色
            selectAll() {
                this.$refs.roleTable.clearSelection();
            },
            open(row) {
                this.$confirm('此操作将永久删除'+row.roleName, '是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete("/role/"+row.id).then(value => {
                        this.loginRoles(this.current)
                    });
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</body>
</html>